Completed
Pull Request — master (#99)
by
unknown
01:05
created

RestClient.put   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
var _ = require('lodash');
2
var Request = require('./request');
3
var q = require('q');
4
5
/**
6
 * Intermediate class to create HTTP requests
7
 *
8
 *
9
 * @param options       object{
10
 *                          host: '',
11
 *                          endpoint: '', // base url for .request
12
 *                          apiKey: 'API_KEY',
13
 *                          apiSecret: 'API_SECRET'
14
 *                      }
15
 * @constructor
16
 * @constructor
17
 */
18
var RestClient = function(options) {
19
    var self = this;
20
21
    self.apiKey = options.apiKey;
22
    self.apiSecret = options.apiSecret;
23
    self.https = options.https;
24
    self.host = options.host;
25
    self.port = options.port;
26
    self.endpoint = options.endpoint;
27
28
    if (options.btccom) {
29
        self.throttleRequestsTimeout = 1500;
30
    } else {
31
        self.throttleRequestsTimeout = 0;
32
    }
33
    self.throttleRequests = self.throttleRequestsTimeout > 0;
34
    self.nextRequest = null;
35
36
    self.defaultParams = {};
37
38
    if (self.apiKey) {
39
        self.defaultParams['api_key'] = self.apiKey;
40
    }
41
42
    self.defaultHeaders = _.defaults({}, {
43
        'X-SDK-Version': 'blocktrail-sdk-nodejs/' + require('./pkginfo').VERSION
44
    }, options.defaultHeaders);
45
};
46
47
RestClient.prototype.throttle = function() {
48
    var self = this;
49
    var deferred = q.defer();
50
51
    if (this.throttleRequests) {
52
        if (this.nextRequest) {
53
            // chain onto the previous delay
54
            this.nextRequest = this.nextRequest.then(function() {
55
                deferred.resolve();
56
57
                return q.delay(self.throttleRequestsTimeout);
58
            });
59
        } else {
60
            // first time we just resolve and setup the delay for the next request
61
            this.nextRequest = q.delay(self.throttleRequestsTimeout);
62
            deferred.resolve();
63
        }
64
    } else {
65
        deferred.resolve();
66
    }
67
68
    return deferred.promise;
69
};
70
71
RestClient.prototype.create_request = function(options) {
72
    var self = this;
73
74
    options = _.defaults({}, options, {
75
        https: self.https,
76
        host: self.host,
77
        port: self.port,
78
        endpoint: self.endpoint,
79
        apiKey: self.apiKey,
80
        apiSecret: self.apiSecret,
81
        params: _.defaults({}, self.defaultParams),
82
        headers: _.defaults({}, self.defaultHeaders)
83
    });
84
85
    return new Request(options);
86
};
87
88
RestClient.prototype.post = function(path, params, data, fn, requireAuth) {
89
    var self = this;
90
91
    requireAuth = typeof requireAuth === "undefined" ? true : requireAuth;
92
93
    var options = {};
94
    if (requireAuth) {
95
        options['auth'] = 'http-signature';
96
    }
97
98
    return self.throttle().then(function() {
99
        return self.create_request(options).request('POST', path, params, data, fn);
100
    });
101
};
102
103
RestClient.prototype.put = function(path, params, data, fn, requireAuth) {
104
    var self = this;
105
106
    requireAuth = typeof requireAuth === "undefined" ? true : requireAuth;
107
108
    var options = {};
109
    if (requireAuth) {
110
        options['auth'] = 'http-signature';
111
    }
112
113
    return self.throttle().then(function() {
114
        return self.create_request(options).request('PUT', path, params, data, fn);
115
    });
116
};
117
118
RestClient.prototype.get = function(path, params, doHttpSignature, fn) {
119
    var self = this;
120
121
    if (typeof doHttpSignature === "function") {
122
        fn = doHttpSignature;
123
        doHttpSignature = false;
124
    }
125
126
    var options = {};
127
128
    if (doHttpSignature) {
129
        options['auth'] = 'http-signature';
130
    }
131
132
    // @TODO: only for during development
133
    if (typeof fn !== "undefined") {
134
        throw new Error("we should be using callbackify!");
135
    }
136
137
    return self.throttle().then(function() {
138
        return self.create_request(options).request('GET', path, params, null, fn);
139
    });
140
};
141
142
RestClient.prototype.delete = function(path, params, data, fn, requireAuth) {
143
    var self = this;
144
145
    requireAuth = typeof requireAuth === "undefined" ? true : requireAuth;
146
147
    var options = {};
148
    if (requireAuth) {
149
        options['auth'] = 'http-signature';
150
    }
151
152
    return self.throttle().then(function() {
153
        return self.create_request(options).request('DELETE', path, params, data, fn);
154
    });
155
};
156
157
module.exports = function(options) {
158
    return new RestClient(options);
159
};
160